//
// Created by afly on 20-12-28.
// Mail: a_fly0505@163.com
// QQ: 785230837 WX: afly003
// Company: juefx
//


#include<iostream>

#include "thread_pool.h"
#include "thread_pool_manager.h"

using namespace std;

namespace qsm {

#define INSTANT    "instant"    //立即执行
//#define EMERGENCY  "emergency"  //突发事件
#define URGENT     "urgent"     //紧急事件
#define GENERAL    "general"    //一般任务

    typedef enum _EventGrade {

    } EventGrade;

    ThreadManager::ThreadManager() {
    }

    ThreadManager::~ThreadManager() {
        m_ThreadPools.clear();
    }

    int ThreadManager::Init() {
        m_ThreadPools.insert(make_pair(INSTANT, make_shared<ThreadPool>(INSTANT)));
        //m_ThreadPools.insert(make_pair(EMERGENCY, make_shared<CThreadPool>(EMERGENCY)));
        m_ThreadPools.insert(make_pair(URGENT, make_shared<ThreadPool>(URGENT)));
        m_ThreadPools.insert(make_pair(GENERAL, make_shared<ThreadPool>(GENERAL)));
        return 0;
    }

    ThreadPoolPtr ThreadManager::GetThreadPool(const char *name) {
        string threadPoolName = name;
        ThreadPoolMap::iterator itera = m_ThreadPools.find(threadPoolName);
        if (itera != m_ThreadPools.end()) {
            return itera->second;
        }
        return nullptr;
    }

    ThreadPoolPtr ThreadManager::GetThreadPoolOrCreate(const char *name) {
        string threadPoolName = name;
        ThreadPoolPtr threadPoolPtr = nullptr;
        ThreadPoolMap::iterator itera = m_ThreadPools.find(threadPoolName);
        if (itera == m_ThreadPools.end()) {
            threadPoolPtr = make_shared<ThreadPool>(threadPoolName.data());
            m_ThreadPools.insert(make_pair(threadPoolName, threadPoolPtr));
        } else {
            threadPoolPtr = itera->second;
        }
        return std::move(threadPoolPtr);
    }

    void ThreadManager::RemoveThreadPool(const char *name) {
        string threadPoolName = name;
        ThreadPoolMap::iterator itera = m_ThreadPools.find(threadPoolName);
        if (itera != m_ThreadPools.end()) {
            std::shared_ptr<ThreadPool> pCThreadPool = itera->second;
            pCThreadPool->Stop();
            m_ThreadPools.erase(itera);
        }
        return;
    }
}